<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>问卷报告管理</el-breadcrumb-item>
      <el-breadcrumb-item>问卷管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 操作区域 -->
      <div>
        <el-button
          class="theme-background-color"
          icon="el-icon-plus"
          size="mini"
          @click="$router.push('/questionnaire/QuestionnaireAdd')"
        >
          新增问卷
        </el-button>
      </div>

      <!-- 列表区域 -->
      <el-table :data="list" border stripe>
        <!-- <el-table-column label="序号" type="index"></el-table-column> -->
        <el-table-column label="问卷编号" prop="code"></el-table-column>
        <el-table-column label="问卷名称" prop="name"></el-table-column>
        <el-table-column label="问卷描述" prop="describe"></el-table-column>
        <el-table-column
          label="创建人"
          prop="create_user_name"
        ></el-table-column>
        <el-table-column label="创建时间" prop="create_date"></el-table-column>
        <el-table-column label="更新时间" prop="update_date"></el-table-column>
        <el-table-column label="状态" prop="status.name"></el-table-column>
        <el-table-column label="操作" width="100px">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="closeQ(scope.row)"
              v-if="scope.row.status.id == 1"
              style="margin-right: 5px"
            >
              关闭
            </el-link>
            <el-link
              type="primary"
              @click="release(scope.row)"
              v-if="scope.row.status.id == 0"
              style="margin-right: 5px"
            >
              发布
            </el-link>
            <el-link
              type="primary"
              v-if="scope.row.status.id == 0"
              @click="
                $router.push(
                  '/questionnaire/QuestionnaireEdit?id=' + scope.row.id
                )
              "
            >
              编辑
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {},
        pagenum: 1,
        pagesize: 20
      },
      list: [],
      total: 0
    }
  },
  created() {
    this.getListData()
  },
  methods: {
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/question_manage/list',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    // 发布问卷
    async release(row) {
      const confirmResult = await this.$confirm(
        '是否确认发布？发布后客户可以在客户端进行该问卷答题',
        '发布问卷',
        {
          confirmButtonText: '确定发布',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消发布')
      }
      const { data: res } = await this.$http.post(
        '/admin/question_manage/updateStatus',
        {
          id: row.id,
          status: 1
        }
      )
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    },
    // 关闭问卷
    async closeQ(row) {
      const confirmResult = await this.$confirm(
        '是否确认关闭？关闭后客户将不能在客户端进行该问卷答题',
        '关闭问卷',
        {
          confirmButtonText: '确定关闭',
          cancelButtonText: '暂不关闭',
          type: 'warning'
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消操作')
      }
      const { data: res } = await this.$http.post(
        '/admin/question_manage/updateStatus',
        {
          id: row.id,
          status: 0
        }
      )
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
